//
//  WBLoginView.swift
//  SinaWeibo_Swift
//
//  Created by TianZhen on 16/4/8.
//  Copyright © 2016年 TianZhen. All rights reserved.
//

import UIKit

protocol WBLoginViewDelegate : NSObjectProtocol
{
    func registBtnClick()  // 点击注册
    func loginBtnClick()   // 点击登录
}

class WBLoginView: UIView {

    
    func registBtnClick()
    {
        delegate?.registBtnClick()
    }
    func loginBtnClick()
    {
        delegate?.loginBtnClick()
    }
    
    // 代理属性
    weak var delegate: WBLoginViewDelegate?
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        // 加载子控件
        setupUI()
    }
    
    // 设置每一页显示的图片和文本内容  
    func setupVisitorViewInfo(imageName: String, message: String)
    {
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        
        coverView.hidden = true
        homeView.hidden = true
    }
    
    
    private func setupUI()
    {
        // 添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 使用自动布局需要关闭 AutoResizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        // iconViewX
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
        
        
        // homeView
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 标题
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 228))
        
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // coverView
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    }
    
    func homeViewScroll()
    {
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
            
            self.iconView.transform = CGAffineTransformRotate(self.iconView.transform, CGFloat(M_PI_4*0.2))
            
            }) { (Bool) -> Void in
                self.homeViewScroll()
        }
    }
    
    // MARK - 懒加载
    // 滚动图片
    private lazy var iconView: UIImageView =
    {
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconView
    }()
    
    // 房子图片
    private lazy var homeView: UIImageView =
    {
        let homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeView
    }()
    
    // 文字
    private lazy var messageLabel: UILabel =
    {
        let messageLabel = UILabel()
        messageLabel.text = "关注一些人,看看有上面惊喜"
        messageLabel.textColor = UIColor.grayColor()
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = NSTextAlignment.Center
        messageLabel.sizeToFit()
        return messageLabel
    }()
    
    // 注册按钮
    private lazy var registBtn: UIButton =
    {
        let registBtn = UIButton()
        registBtn.setTitle("注册", forState: UIControlState.Normal)
        registBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 点击事件
        registBtn.addTarget(self, action: "registBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return registBtn
    }()
    
    // 登录按钮
    private lazy var loginBtn: UIButton =
    {
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return loginBtn
    }()
    
    // 遮罩
    private lazy var coverView: UIImageView =
    {
        let coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return coverView
    }()
}
